package net.oschina.app.fragment.explore;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.R;
import net.oschina.app.adapter.explore.RecentlyAdapter;
import net.oschina.app.constant.Constants;
import net.oschina.app.bean.Event;
import net.oschina.app.bean.EventList;
import net.oschina.app.fragment.BaseFragment;
import net.oschina.app.listener.OnRecycleViewItemClickListener;
import net.oschina.app.ui.EventActivty;
import net.oschina.app.util.URLGet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wuer on 2017/4/8/008.
 * 活动子页面的基类
 */

public abstract class BaseEventVpFragment extends BaseFragment {
    private List<Event> mLists = new ArrayList<Event>();
    private XRecyclerView mRv;
    protected RecentlyAdapter mRecentlyAdapter;
    @Override
    protected View initView() {
        mRv = (XRecyclerView) View.inflate(getContext(), R.layout.news_rv, null);
        mRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        return mRv;
    }

    @Override
    protected void setData() {
        URLGet urlGet = new URLGet();
        int index = 0;
        String s = Constants.Events_Path + getIndex() + ".xml";
        urlGet.getData(s, EventList.class, new URLGet.OnURLGetLitener<EventList>() {
            @Override
            public void showSuccess(EventList bean) {
                EventList meventsList = bean;
                mLists = meventsList.getList();
                dealCommonSuccess(mLists);
            }

            @Override
            public void showError(Exception e) {
                dealCommonFail();
            }
        });
    }

    @Override
    protected void showSuccess() {
        mRecentlyAdapter = new RecentlyAdapter(mLists);
        mRv.setAdapter(mRecentlyAdapter);
        //添加头部
        // initRecyclerHeadView(mRv);
        // 静止下拉刷新，加载更多
        mRv.setLoadingMoreEnabled(true);
        mRv.setPullRefreshEnabled(false);
        loadMore(mRv);// 加载更多
        initEvent(); // 处理事件
        changeView(mRv);// 更新处理
    }
    //初始化事件
    private void initEvent() {
        mRecentlyAdapter.setOnItemClickListener(new OnRecycleViewItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                position = position - 1;
                Log.d("position:", "-----" + position);
                Intent intent = new Intent(getActivity(), EventActivty.class);
                Event events = mLists.get(position);
                int id = events.getId();
                intent.putExtra("id", id);
                startActivity(intent);
            }
        });
    }

    // 添加头部
    private void initRecyclerHeadView(XRecyclerView mRv) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View headView = inflater.inflate(R.layout.news_rv_header_view, mRv, false);
        mRv.addHeaderView(headView);
    }

    //加载更多
    private void loadMore(XRecyclerView mRv) {
        mRv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
            }

            @Override
            public void onLoadMore() {
                URLGet urlGet = new URLGet();
                int index = 0;
                urlGet.getData(Constants.Events_Path + index + ".xml", EventList.class, new URLGet.OnURLGetLitener<EventList>() {
                    @Override
                    public void showSuccess(EventList bean) {
                        EventList meventsList = bean;
                        List<Event> mLists1 = meventsList.getList();
                        mLists.addAll(mLists1);
                        mRecentlyAdapter.notifyDataSetChanged();
                    }
                    @Override
                    public void showError(Exception e) {
                        dealCommonFail();
                    }
                });
            }
        });
    }
    protected abstract String getIndex();
}
